//
//  DestionViewController.m
//  Tour
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "DestionViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "AdvanceEdit.h"
#import "ZGSDestinationModel.h"
#import "ZGSDestinationCity.h"
#import "ZGSMyCell.h"
#import "ZGSDestinationCityController.h"
#import "ZGSDestinationCell.h"
#import "SaveTool.h"
#import "ZGSQQQQQQQQQQ.h"
#import "ZGSDataBaseHandle.h"
@interface DestionViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UIView *headView;
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *arrCounty;
@property (nonatomic, retain)MBProgressHUD *HUD;

@end

@implementation DestionViewController

- (void)dealloc{
    [_tableView release];
    [_arr release];
    [_headView release];
    [_imageView release];
    [_collectionView release];
    [_arrCounty release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"目的地";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableReuse"];
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    headView.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 2.5  + HEIGHT / 2.5 * 4 + 90 + 50);
    [self.view addSubview:headView];
    [headView release];
    
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"8c37432472eeb5273f696060f445927a.jpg"]];
    
    UIView *viewButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 2.5 + 50)];
    viewButton.backgroundColor = [UIColor whiteColor];
    viewButton.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [headView addSubview:viewButton];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:viewButton.frame];
    imageView.image = [UIImage imageNamed:@"ditu.jpg"];
    [viewButton addSubview:imageView];
    [imageView release];
    
//    UILabel *label = [[UILabel alloc] init];
//    label.frame = CGRectMake(0, HEIGHT / 2.5, WIDTH, 50);
//    label.text = @"热门目的地";
//    label.textColor = [UIColor grayColor];
//    label.font = [UIFont systemFontOfSize:14];
//    [headView addSubview:label];
    
    
    UIButton *buttonAsia = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonAsia.frame = CGRectMake(WIDTH / 6, 50, 100, 50);
    buttonAsia.tintColor = [UIColor blackColor];
    [buttonAsia setTitle:@"亚洲" forState:UIControlStateNormal];
    buttonAsia.tag = 2000;
//    button0.layer.borderWidth = 1;
    [buttonAsia addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [viewButton addSubview:buttonAsia];
    
    
    UIButton *buttonEurope = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonEurope.frame = CGRectMake(WIDTH / 6, 110, 100, 50);
    buttonEurope.tintColor = [UIColor blackColor];
    [buttonEurope setTitle:@"欧洲" forState:UIControlStateNormal];
    buttonEurope.tag = 2001;
//    button1.layer.borderWidth = 1;
    [buttonEurope addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [viewButton addSubview:buttonEurope];

    UIButton *buttonAfrica = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonAfrica.frame = CGRectMake(WIDTH / 6, 180, 100, 50);
    [buttonAfrica setTitle:@"非洲" forState:UIControlStateNormal];
//    button2.layer.borderWidth = 1;
    buttonAfrica.tintColor = [UIColor blackColor];
    buttonAfrica.tag = 2005;
    [buttonAfrica addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [viewButton addSubview:buttonAfrica];
    
    UIButton *buttonNorth = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonNorth.frame = CGRectMake(WIDTH - 150, 50, 100, 50);
    [buttonNorth setTitle:@"北美洲" forState:UIControlStateNormal];
//    button3.layer.borderWidth = 1;
    buttonNorth.tintColor = [UIColor blackColor];
    [buttonNorth addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    buttonNorth.tag = 2002;
    [viewButton addSubview:buttonNorth];
    
    UIButton *buttonSouth = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonSouth.frame = CGRectMake(WIDTH - 150, 110, 100, 50);
    [buttonSouth setTitle:@"南美洲" forState:UIControlStateNormal];
//    button4.layer.borderWidth = 1;
    buttonSouth.tintColor = [UIColor blackColor];
    buttonSouth.tag = 2003;
    [buttonSouth addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [viewButton addSubview:buttonSouth];
    
    UIButton *buttonOceania = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOceania.frame = CGRectMake(WIDTH - 150, 180, 100, 50);
    [buttonOceania setTitle:@"大洋洲" forState:UIControlStateNormal];
//    button5.layer.borderWidth = 1;
    buttonOceania.tintColor = [UIColor blackColor];
    buttonOceania.tag = 2004;
    [buttonOceania addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [viewButton addSubview:buttonOceania];
    
    UILabel *labelQita = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 2.5 + 50 + HEIGHT / 2.5 * 4 + 40, WIDTH, 50)];
    labelQita.textColor = [UIColor grayColor];
    labelQita.text = @"其他目的地";
//    labelQita.font = [UIFont systemFontOfSize:14];
    [headView addSubview:labelQita];

    
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 10, HEIGHT / 2.5);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 2.5 + 50, WIDTH, HEIGHT / 2.5 * 4 + 40) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[ZGSMyCell class] forCellWithReuseIdentifier:@"reuse"];
    [headView addSubview:self.collectionView];
    [_collectionView release];
    self.tableView.tableHeaderView = headView;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.dimBackground = YES;
    
//    [SaveTool isHaveFolder];

    [self creatData];
    
    
    
}
- (void)creatData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88266923325023&lon=121.5394170925639&page=1&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone7%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%25208.4.1&v=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [NSMutableArray array];
        self.arrCounty = [NSMutableArray array];
        self.arrCounty = [ZGSDestinationCity baseModelArr:dic[@"data"][0][@"hot_country"]];
        self.arr = [ZGSDestinationCity baseModelArr:dic[@"data"][0][@"country"]];
        for (ZGSDestinationCity *destina in self.arrCounty) {
            if ([destina.flag  isEqual: @2]) {
                [self.arrCounty removeObject:destina];
                break;
            }
        }
        for (ZGSDestinationCity *destina in self.arr) {
            if ([destina.flag  isEqual: @2]) {
                [self.arr removeObject:destina];
                break;
            }
        }

        self.HUD.hidden = YES;
        [self.tableView reloadData];
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    
}

- (void)click:(UIButton *)button{
    for ( NSInteger i = 0; i <= 5; i++) {
        if (button.tag == i + 2000) {
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            [manager GET:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88266923325023&lon=121.5394170925639&page=1&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone7%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%25208.4.1&v=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *dic = responseObject;
                NSLog(@"%@", dic);
                self.arr = [NSMutableArray array];
                self.arrCounty = [NSMutableArray array];
                self.arrCounty = [ZGSDestinationCity baseModelArr:dic[@"data"][i][@"hot_country"]];
                self.arr = [ZGSDestinationCity baseModelArr:dic[@"data"][i][@"country"]];
                for (ZGSDestinationCity *destina in self.arrCounty) {
                    if ([destina.flag  isEqual: @2]) {
                        [self.arrCounty removeObject:destina];
                        break;
                    }
                }
                for (ZGSDestinationCity *destina in self.arr) {
                    if ([destina.flag  isEqual: @2]) {
                        [self.arr removeObject:destina];
                        break;
                    }
                }

                [self.tableView reloadData];
                [self.collectionView reloadData];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
                
            }];
            

          
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableReuse" forIndexPath:indexPath];
    cell.textLabel.text = [self.arr[indexPath.row] cnname];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDestinationCityController *cityVC = [[ZGSDestinationCityController alloc] init];
    ZGSDestinationCity *city = [[ZGSDestinationCity alloc] init];
    city = self.arr[indexPath.row];
    NSString *tmpUrl = [NSString stringWithFormat:@"%@", city.countryid];
    NSLog(@"%@", city.countryid);
    cityVC.cityid = tmpUrl;
    cityVC.name = city.cnname;
    cityVC.hidesBottomBarWhenPushed = YES;
    ZGSQQQQQQQQQQ *movie = [[ZGSQQQQQQQQQQ alloc] init];
     movie = self.arr[indexPath.row];
    
    cityVC.movie = movie;

    [self.navigationController pushViewController:cityVC animated:YES];
    
    

    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDestinationCityController *cityVC = [[ZGSDestinationCityController alloc] init];
    ZGSDestinationCity *city = [[ZGSDestinationCity alloc] init];
    city = self.arrCounty[indexPath.row];
        NSString *tmpUrl = [NSString stringWithFormat:@"%@", city.countryid];
        NSLog(@"%@", city.countryid);
        cityVC.cityid = tmpUrl;
    cityVC.name = city.cnname;
    ZGSQQQQQQQQQQ *movie = [[ZGSQQQQQQQQQQ alloc] init];

    movie = self.arrCounty[indexPath.row];
    cityVC.movie = movie;

        cityVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:cityVC animated:YES];
//    }

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrCounty.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    [[ZGSDataBaseHandle shareDataBaseHandle] createTable];
    ZGSMyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell.imageCountry sd_setImageWithURL:[NSURL URLWithString:[self.arrCounty[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
    cell.labelCity.text = [self.arrCounty[indexPath.row] cnname];
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
